package com.diankangwo.service.impl;

import com.diankangwo.dao.OnlinePayLogDAO;
import com.diankangwo.domain.OnlinePayLog;
import com.diankangwo.dto.OnlinePayLogDTO;
import com.diankangwo.service.OnlinePayLogService;
import com.diankangwo.util.EntityDtoConverter;
import com.diankangwo.util.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 
 * @author Mr.zhu
 *
 */
@Service
public class OnlinePayLogServiceImpl implements OnlinePayLogService {
	
	Logger LOG = Logger.getLogger(this.getClass());

	@Autowired
	private OnlinePayLogDAO onlinePayLogDAO;

	@Override
	public boolean insertLog(OnlinePayLogDTO logDTO) {
		try {
			logDTO.setIdKey(UUID.randomUUID().toString());
			OnlinePayLog log = new OnlinePayLog();
			EntityDtoConverter ed = new EntityDtoConverter();
			log = (OnlinePayLog)ed.DtoConvertEntity(log,logDTO);
			onlinePayLogDAO.insert(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean updateLog(OnlinePayLogDTO logDTO) {
		try {
			OnlinePayLog log = new OnlinePayLog();
			EntityDtoConverter ed = new EntityDtoConverter();
			log = (OnlinePayLog)ed.DtoConvertEntity(log,logDTO);
			onlinePayLogDAO.update(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Override
	public boolean saveOrUpdate(OnlinePayLogDTO logDTO) {
		try {
			OnlinePayLog log = new OnlinePayLog();
			EntityDtoConverter ed = new EntityDtoConverter();
			log = (OnlinePayLog)ed.DtoConvertEntity(logDTO,log);
			onlinePayLogDAO.saveOrUpdate(log);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public OnlinePayLogDTO serchLogById(Serializable id) {
		try {
			OnlinePayLog log = onlinePayLogDAO.find(id);
			OnlinePayLogDTO logDTO = new OnlinePayLogDTO();
			EntityDtoConverter ed = new EntityDtoConverter();
			logDTO = (OnlinePayLogDTO)ed.entityConvertDto(log,logDTO);

			return logDTO;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Override
	public OnlinePayLogDTO searchLogByBillNo(String billNo) {
		try {
			OnlinePayLog log = onlinePayLogDAO.findByProperty("billNo", billNo).get(0);
			OnlinePayLogDTO logDTO = new OnlinePayLogDTO();
			EntityDtoConverter ed = new EntityDtoConverter();
			logDTO = (OnlinePayLogDTO)ed.entityConvertDto(log,logDTO);
			return logDTO;
		} catch (Exception e) {
			LOG.warn("支付记录中无此订单号："+billNo);
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public List<OnlinePayLogDTO> serchAllLog() {
		try {
			List<OnlinePayLog> oplList = onlinePayLogDAO.findAll();
			List<OnlinePayLogDTO> opldList = new ArrayList<OnlinePayLogDTO>() ;
			for (OnlinePayLog log : oplList) {
				OnlinePayLogDTO dto = new OnlinePayLogDTO();
				EntityDtoConverter ed = new EntityDtoConverter();
				dto = (OnlinePayLogDTO)ed.entityConvertDto(log,dto);
				opldList.add(dto);
			}
			return opldList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public PageSupport<OnlinePayLogDTO> searchLogByPage(Integer pageNo, Integer pageSize) {
		try {
			PageSupport<OnlinePayLog> psopl =  onlinePayLogDAO.findPagination(pageNo, pageSize);
			PageSupport<OnlinePayLogDTO> psopld = new PageSupport<OnlinePayLogDTO>();
			List<OnlinePayLogDTO> opldList = new ArrayList<OnlinePayLogDTO>() ;
			for (OnlinePayLog log : psopl.getResultData()) {
				OnlinePayLogDTO dto = new OnlinePayLogDTO();
				EntityDtoConverter ed = new EntityDtoConverter();
				dto = (OnlinePayLogDTO)ed.entityConvertDto(log,dto);
				opldList.add(dto);
			}
//			psopld.setResultData(opldList);
//			psopld.setStart(psopl.getStart());
			return psopld;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


}
